package org.sxf.app.sign.utils;

import com.lidroid.xutils.util.LogUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.UUID;

/**
 * Created by mhy8958 on 2015/5/5.
 * */
public class Utility {

    public static byte reverseHexString(byte paramByte){
        return (byte) ~paramByte;
    }
    public static byte[] hexStringToBytes(String paramString){
        int i = paramString.length();
        byte[] arrayOfByte = new byte[i / 2];
        for (int j = 0; ; j += 2){
            if (j >= i)
                return arrayOfByte;
            arrayOfByte[(j / 2)] = ((byte)((Character.digit(paramString.charAt(j), 16) << 4) + Character.digit(paramString.charAt(j + 1), 16)));
        }
    }
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static String byteToHexString(byte b){
        StringBuilder stringBuilder = new StringBuilder("");
        int v = b & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
        return stringBuilder.toString();
    }
    public static String hexToDec(String paramString){
        return Integer.parseInt(paramString, 16)+"";
    }
    public static String NG_PROCESS_STATE(String ps){
        if(ps.length()==2&& ps.charAt(0)=='0')
        {
            return "1"+ps.substring(1);
        }
        return null;
    }
    public static String TIMEOUT_PROCESS_STATE(String ps){
        if(ps.length()==2&& ps.charAt(0)=='0')
        {
            return "2"+ps.substring(1);
        }
        return null;
    }

    public static String generateUUID(){
        String s = UUID.randomUUID().toString();
        return s.replace("-","");
    }

    public static String getStackTrace(Throwable t){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try{
            t.printStackTrace(pw);
            return sw.toString();
        }finally {
            pw.close();
        }
    }

    public static String getCutStackTrace(Throwable t){
        String result = getStackTrace(t);
        try {
            if (result.length() >=1000){
                result = result.substring(0, 1000);
            }
        }catch (Exception e){

        }
        return result;
    }


    public static String phoneNumSecret(String phoneNum){
        try {
            return phoneNum.substring(0, 3) + "****" + phoneNum.substring(7, 11);
        }catch (Exception e){
            return phoneNum;
        }
    }

    public static String nameSecret(String name){
        try {
//            int nameLength = name.length();
//            String star = "";
//            for (int i = 0; i<nameLength; i++){
//                star += "*";
//            }
            return "*" + name.substring(1);
        }catch (Exception e){
            return name;
        }
    }

}
